package com.woniuxy.factory;

import com.woniuxy.person.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class BeanFactory {
    static String name;
    static Map<String, Object> map = new HashMap<>();
    static {
        InputStream resourceAsStream = BeanFactory.class.getResourceAsStream("person.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (String stringPropertyName : properties.stringPropertyNames()) {
            Object values = properties.getProperty(stringPropertyName);
            map.put(stringPropertyName, values);
        }
    }

    public Object getBean(String name) {
        Object o = map.get(name);
        try {
            Person person = (Person) Class.forName(o + "").newInstance();
            return person;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}


